{% extends 'base.html' %}

{% block content %}
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
        <legend id="titleLeg">防火墙</legend>
    </fieldset>

    <table class="layui-hide" id="test" lay-filter="test"></table>

{% endblock %}

{% block script %}
    {{ super() }}
    <script type="text/html" id="barDemo">
        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <script>
        var tablaneme = document.location.pathname.split('/')[3];
        var position = document.location.pathname.split('/')[4];
        {#var urls = '/fwmap/fwIndexApi/' + tablaneme + '/' + position;#}
        Title(4);

        layui.use('table', function () {
            var table = layui.table;
            // 需要先定义， 直接在render中定义没用
            table.render({
                elem: '#test'
                , url: '/fwmap/fwIndexApi/' + tablaneme + '/' + position
                , width: 400
                , title: '地址表'
                , cols: [[
                    {field: 'id', title: 'ID', width: 120, edit: 'text', sort: true}
                    , {field: 'address', title: '地址', width: 120, sort: true}
                    , {fixed: 'right', title: '操作', toolbar: '#barDemo'}
                ]]
                ,
                page: true
            });

            //监听行工具事件
            table.on('tool(test)', function (obj) {
                var data = obj.data;
                //console.log(obj)
                if (obj.event === 'del') {
                    layer.confirm('真的删除行么', function (index) {
                        $.ajax({
                            url: '/fwmap/fwIndexApi/' + tablaneme + '/' + position,
                            type: 'DELETE',
                            async: false,
                            data: {"id": data.id},
                            success: function (data) {
                                if (data.code == 10000) {
                                    obj.del();
                                    layer.close(index);
                                    layer.confirm('删除成功');
                                } else {
                                    layer.confirm('删除失败,\n 请先删除相关连的映射条目');
                                }
                            }
                        })
                    });
                } else if (obj.event === 'edit') {
                    var urls = '/fwmap/fwedit/' + tablaneme + '/' + position + '/' + data.id;
                    window.open(urls, '_self');
                }
            });

        });
    </script>

{% endblock %}